﻿using CJiaBao.Core.Utils;
using Core.Configuration.Api;
using Core.Configuration.Config;
using Core.Configuration.Helper;
using IconYuan.Manager.Stores;
using IconYuan.Model.DTO.Stores;
using IconYuan.Model.Entity.Stores;
using Microsoft.AspNetCore.Mvc;

namespace IconYuan.Api.Controllers.Stores
{
    /// <summary>
    /// 店铺操作
    /// </summary>
    public class StoreController : ApiBase
    {
        readonly StoreManager storeSvc;
        readonly ShopManager shopSvc;
        public StoreController()
        {
            storeSvc = new StoreManager();
            shopSvc = new ShopManager();
        }

        /// <summary>
        /// 首页数据
        /// </summary>
        /// <param name="lat">坐标</param>
        /// <param name="lng">坐标</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [HttpGet]
        public async Task<IndexDataDto> Data(double lat = 0, double lng = 0)
        {
            var store_id = GetStoreId();
            var result = new IndexDataDto();

            var list = await shopSvc.GetListAsync<ShopBase>(x => x.store_id == store_id && x.status == 1);
            if (list.Count <= 0) throw new Exception("缺少门店，请核实门店门店是否存在");
            
            //距离最近门店
            if (lat > 0)
            {
                list.ForEach(x =>
                {
                    x.distance = (int)LocationHelper.GetDistance(lat, lng, x.latitude, x.longitude);
                });
                result.shop = list.OrderBy(x => x.distance).ToList().First();
            }                
            else  //默认门店
                result.shop = await shopSvc.GetAsync<ShopBase>(x => x.store_id == store_id && x.status == 1);

            result.storeInfo = await storeSvc.GetAsync(store_id);
            if (!string.IsNullOrEmpty(result.storeInfo?.logos))
            {
                result.banner = JsonHelper.ToObject<List<string>>(result.storeInfo.logos)
                        .Select(logo => $"{GlobalConfig.CdnDomain}{logo}").ToList();

                result.storeInfo.logoList = result.banner;
            }
            return result;
        }

        /// <summary>
        /// 门店列表
        /// </summary>
        /// <param name="lat">坐标</param>
        /// <param name="lng">坐标</param>
        /// <param name="page">当前页，默认 1</param>
        /// <param name="pageSize">每页几条，默认 15</param>
        /// <returns></returns>
        [HttpGet]
        public object ShopList(double lat = 0, double lng = 0, int page = 1, int pageSize = 15)
        {
            var store_id = GetStoreId();
            var result = shopSvc.GetPageList<ShopBase>(x => x.store_id == store_id && x.status == 1);
            //当前门店
            if (lat > 0)
            {
                result.data.ForEach(x =>
                {
                    x.distance = (int)LocationHelper.GetDistance(lat, lng, x.latitude, x.longitude);
                });
                result.data = result.data.OrderBy(x => x.distance).ToList();
            }
            
            return result;
        }
    }
}
